package com.totsp.crossword;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.view.Gravity;
import android.widget.Toast;

import com.totsp.crossword.puz.Puzzle;

/**
 * Background task to unlock a scrambled puzzle.
 */
public class UnlockTask extends AsyncTask<Void, Integer, Boolean> {
    public static final int TOTAL_POSSIBLE = 9*9*9*9;

    private final PlayActivity playActivity;
    private final Puzzle puz;
    private final ProgressDialog unscrambleDialog;

    public UnlockTask(PlayActivity playActivity) {
        this.playActivity = playActivity;
        puz = playActivity.getPuzzle();
        unscrambleDialog = playActivity.getUnscrambleDialog();
    }

    @Override
    protected void onPreExecute() {
        if (playActivity.getTimer() != null) {
            playActivity.getTimer().stop();
        }
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        byte[] solution = puz.initializeUnscrambleData();
        byte[] attempt = new byte[solution.length];
        boolean success;
        System.arraycopy(solution, 0, attempt, 0, solution.length);
        byte[] best = new byte[solution.length];
        int tried = 0;
        int maxCandidateVowels = -1;
        for (short i = 1111; i <= 9999; i++) {
            if (i % 10 != 0 && (i/10) % 10 != 0 && (i/100) % 10 != 0) {
                System.arraycopy(solution, 0, attempt, 0, solution.length);
                success = puz.tryUnscramble(i, attempt);
                if (success) {
                    int vowels = new String(attempt).split("[AEIOU]",-1).length;
                    if (vowels >= maxCandidateVowels) {
                        maxCandidateVowels = vowels;
                        System.arraycopy(attempt, 0, best, 0, attempt.length);
                    }
                }
                publishProgress(tried++);
            }
        }
        if (maxCandidateVowels >= 0) {
            puz.setUnscrambledSolution(best);
            return true;
        }
        return false;
    }

    @Override
    protected void onProgressUpdate(Integer... params) {
        unscrambleDialog.setProgress(params[0]);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        unscrambleDialog.dismiss();
        String msg;
        if (result) {
            msg = "Unscrambled successfully!";
            boolean showErrors = playActivity.prefs.getBoolean("showErrors", false);
            playActivity.setShowErrors(showErrors);
            PlayActivity.BOARD.setShowErrors(showErrors);
            playActivity.getUtils().invalidateOptionsMenu(playActivity);
        } else {
            msg = "Unable to unscramble puzzle.";
        }
        Toast t = Toast.makeText(playActivity, msg, Toast.LENGTH_SHORT);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.show();
        if (playActivity.getTimer() != null) {
            playActivity.getTimer().start();
        }
    }
}
